<?php
namespace app\index\controller;
use think\facade\Db;
use think\facade\View;
use think\facade\Cookie;
use think\facade\Request;

class My{
	public $uid = null;
	public function __construct(){
		$this->uid = Cookie::get('uid');
		if(empty($this->uid)){
			echo '<script>alert("请先登录");window.location.href="/index.php/index/index/login";</script>';
			return false;
		}
	}
	public function index(){
		$find = Db::table('oyk_user')->where('uid',$this->uid)->find();
		$school = Db::table('oyk_school')->where('uid',$this->uid)->find();
		View::assign([
			'find' => $find,
			'school' => $school
		]);
		return View::fetch();
	}
	public function enroll(){
		$id = (int)input('get.id',0);
		if(empty($id)){
			echo '<script>alert("未找到班级");window.history.back(-1);</script>';
			return false;
		}
		$class = Db::table('oyk_school_class')->where('id',$id)->where('status',1)->find();
		if(empty($class)){
			echo '<script>alert("未找到班级");window.history.back(-1);</script>';
			return false;
		}
		$time = time();
		$class_end_date = strtotime($class['class_end_date']);

		if($class_end_date < $time){
			echo '<script>alert("报名已截止");window.history.back(-1);</script>';
			return false;
		}
		$user_name = Db::table('oyk_user_name')->where('uid',$this->uid)->select();
		View::assign([
			'id' => $id,
			'user_name' => $user_name
		]);
		return View::fetch();
	}
	public function enroll_name_json(){
		$id = input('post.id');
		$find = Db::table('oyk_user_name')->where('id',$id)->find();
		if(empty($find)){
			$this->returnCode(1,'未找到名单');
		}
		$this->returnCode(0,$find);
	}
	public function enroll_json(){
		$id = (int)input('post.id');
		if(empty($id)){
			$this->returnCode(1,'未找到班级');
		}
		$baby_name = input('post.baby_name');
		if(empty($baby_name)){
			$this->returnCode(1,'请输入孩子姓名');
		}
		$baby_gender = input('post.baby_gender');
		$baby_age = input('post.baby_age');
		$baby_school = input('post.baby_school');
		if(empty($baby_school)){
			$this->returnCode(1,'请输入孩子学校');
		}
		$baby_grade = input('post.baby_grade');
		if(empty($baby_grade)){
			$this->returnCode(1,'请输入孩子年级');
		}
		$baby_class = input('post.baby_class');
		if(empty($baby_class)){
			$this->returnCode(1,'请输入孩子班级');
		}
		$parent_name = input('post.parent_name');
		if(empty($parent_name)){
			$this->returnCode(1,'请输入家长姓名');
		}
		$parent_phone = input('post.parent_phone');
		if(empty($parent_phone)){
			$this->returnCode(1,'请输入家长手机号');
		}
		$data = [
			'class_id' => $id,
			'uid' => $this->uid,
			'baby_name' => $baby_name,
			'baby_gender' => $baby_gender,
			'baby_age' => $baby_age,
			'baby_school' => $baby_school,
			'baby_grade' => $baby_grade,
			'baby_class' => $baby_class,
			'parent_name' => $parent_name,
			'parent_phone' => $parent_phone,
			'add_time' => time()
		];
		$id = Db::table('oyk_user_enroll')->insertGetId($data);
		if(empty($id)){
			$this->returnCode(1,'报名失败，请重试');
		}

		$select = (int)input('post.select');
		if(empty($select)){
			Db::table('oyk_user_name')->insert([
				'uid' => $this->uid,
				'baby_name' => $baby_name,
				'baby_gender' => $baby_gender,
				'baby_age' => $baby_age,
				'baby_school' => $baby_school,
				'baby_grade' => $baby_grade,
				'baby_class' => $baby_class,
				'parent_name' => $parent_name,
				'parent_phone' => $parent_phone,
			]);
		}
		$this->returnCode(0);
	}
	public function my_enroll(){
		return View::fetch();
	}
	public function my_enroll_json(){
		$page = (int)input('post.page',1);
		$enroll = Db::table('oyk_user_enroll')->where('uid',$this->uid)->order('add_time DESC')->page($page,10)->select()->toArray();
		$tmp_class_id = [];
		if(!empty($enroll)){
			foreach($enroll as $enroll_v){
				$class_id[] = $enroll_v['class_id'];
			}
			$class = Db::table('oyk_school_class')->where('id','in',$class_id)->column('*','id');
			foreach($enroll as &$enroll_v){
				$enroll_v['class'] = $class[ $enroll_v['class_id'] ];
			}
		}
		
		$this->returnCode(0,$enroll);
	}
	public function name_list(){
		return View::fetch();
	}
	public function name_list_json(){
		$page = (int)input('post.page',1);
		$user_name = Db::table('oyk_user_name')->where('uid',$this->uid)->order('id DESC')->page($page,10)->select()->toArray();
		$this->returnCode(0,$user_name);
	}
	public function name_edit(){
		$id = input('get.id');
		$find = Db::table('oyk_user_name')->where('id',$id)->find();
		View::assign([
			'id' => $id,
			'find' => $find
		]);
		return View::fetch();
	}
	public function name_edit_json(){
		$id = (int)input('post.id');
		if(empty($id)){
			$this->returnCode(1,'未找到班级');
		}
		$baby_name = input('post.baby_name');
		if(empty($baby_name)){
			$this->returnCode(1,'请输入孩子姓名');
		}
		$baby_gender = input('post.baby_gender');
		$baby_age = input('post.baby_age');
		$baby_school = input('post.baby_school');
		if(empty($baby_school)){
			$this->returnCode(1,'请输入孩子学校');
		}
		$baby_grade = input('post.baby_grade');
		if(empty($baby_grade)){
			$this->returnCode(1,'请输入孩子年级');
		}
		$baby_class = input('post.baby_class');
		if(empty($baby_class)){
			$this->returnCode(1,'请输入孩子班级');
		}
		$parent_name = input('post.parent_name');
		if(empty($parent_name)){
			$this->returnCode(1,'请输入家长姓名');
		}
		$parent_phone = input('post.parent_phone');
		if(empty($parent_phone)){
			$this->returnCode(1,'请输入家长手机号');
		}
		$data = [
			'baby_name' => $baby_name,
			'baby_gender' => $baby_gender,
			'baby_age' => $baby_age,
			'baby_school' => $baby_school,
			'baby_grade' => $baby_grade,
			'baby_class' => $baby_class,
			'parent_name' => $parent_name,
			'parent_phone' => $parent_phone
		];
		$id = Db::table('oyk_user_name')->where('id',$id)->update($data);
		if(empty($id)){
			$this->returnCode(1,'修改失败，请重试');
		}
		$this->returnCode(0);
	}
	public function teacher(){
		$find = Db::table('oyk_school')->where('uid',$this->uid)->find();
		if(!empty($find) && $find['status'] == 0 && $find['status'] == 9){
			echo '<script>alert("您的企业(学校)已被关闭，请联系管理员");window.history.back(-1);</script>';
			return false;
		}
		View::assign([
			'find' => $find
		]);
		return View::fetch();
	}
	public function teacher_json(){
		$school_name = input('post.school_name');
		if(empty($school_name)){
			$this->returnCode(1,'请输入企业(学校)名');
		}
		$name = input('post.name');
		if(empty($name)){
			$this->returnCode(1,'请输入联系人');
		}
		$phone = input('post.phone');
		if(empty($phone)){
			$this->returnCode(1,'请输入联系手机');
		}
		$license = input('post.license');
		if(empty($license)){
			$this->returnCode(1,'请上传证件');
		}
		$find = Db::table('oyk_school')->where('uid',$this->uid)->find();
		if(empty($find)){
			$insert = Db::table('oyk_school')->insert([
				'school_name' => $school_name,
				'name' => $name,
				'phone' => $phone,
				'license' => $license,
				'uid' => $this->uid,
				'add_time' => time(),
				'status' => 2
			]);
		}else{
			$insert = Db::table('oyk_school')->where('uid',$this->uid)->update([
				'school_name' => $school_name,
				'name' => $name,
				'phone' => $phone,
				'license' => $license,
				'status' => 2
			]);
		}
		
		if(empty($insert)){
			$this->returnCode(1,'申请失败，请重试');
		}
		$this->returnCode(0);
	}
	public function course(){
		$school = Db::table('oyk_school')->where('uid',$this->uid)->find();
		if(empty($school) || $school['status'] != 1){
			echo '<script>alert("您还未成为老师");window.history.back(-1);</script>';
			return false;
		}
		return View::fetch();
	}
	public function course_json(){
		$school = Db::table('oyk_school')->where('uid',$this->uid)->find();
		if(empty($school)  || $school['status'] != 1){
			$this->returnCode(1,'您还未成为老师');
		}
		$page = (int)input('post.page',1);
		$class = Db::table('oyk_school_class')->where('school_id',$school['school_id'])->order('add_time DESC')->page($page,10)->select()->toArray();
		$this->returnCode(0,$class);
	}
	public function course_add(){
		$school = Db::table('oyk_school')->where('uid',$this->uid)->find();
		if(empty($school) || $school['status'] != 1){
			echo '<script>alert("您还未成为老师");window.history.back(-1);</script>';
			return false;
		}
		$id = input('get.id',0);
		$find = [];
		if($id != 0){
			$find = Db::table('oyk_school_class')->where('id',$id)->find();
			if(!empty($find)){
				$find['intr_s'] = explode(';',$find['intr']);
			}
		}
		View::assign([
			'id' => $id,
			'find' => $find
		]);
		return View::fetch();
	}
	public function course_add_json(){
		$id = input('post.id',0);
		$find = Db::table('oyk_school')->where('uid',$this->uid)->find();
		if(empty($find) || $find['status'] != 1){
			$this->returnCode(1,'您还未成为老师');
		}
		$title = input('post.title');
		if(empty($title)){
			$this->returnCode(1,'请输入标题');
		}
		$tuition = input('post.tuition',0);
		$people_num = input('post.people_num',0);

		$teacher_img = input('post.teacher_img');
		$teacher_name = input('post.teacher_name');
		if(empty($teacher_name)){
			$this->returnCode(1,'请输入老师姓名');
		}
		$teacher_phone = input('post.teacher_phone');
		if(empty($teacher_phone)){
			$this->returnCode(1,'请输入老师电话');
		}
		$class_start_date = input('post.class_start_date');
		if(empty($class_start_date)){
			$this->returnCode(1,'请选择上课时间');
		}
		$class_time = input('post.class_time');
		if(empty($class_time)){
			$this->returnCode(1,'请输入上课周期');
		}
		$class_hour = input('post.class_hour');
		if(empty($class_hour)){
			$this->returnCode(1,'请输入课时');
		}
		$class_end_date = input('post.class_end_date');
		if(empty($class_end_date)){
			$this->returnCode(1,'请输入报名截止时间');
		}
		$address = input('post.address');
		if(empty($address)){
			$this->returnCode(1,'请输入上课教室');
		}
		$intr = input('post.intr');
		if(!empty($intr)){
			$intr = implode(';',$intr);
		}

		$data = [
			'title' => $title,
			'tuition' => $tuition,
			'people_num' => $people_num,
			'teacher_img' => $teacher_img,
			'teacher_name' => $teacher_name,
			'teacher_phone' => $teacher_phone,
			'class_start_date' => $class_start_date,
			'class_time' => $class_time,
			'class_hour' => $class_hour,
			'class_end_date' => $class_end_date,
			'address' => $address,
			'intr' => $intr,
			'school_id' => $find['school_id']
		];

		if($id == 0){
			$data['add_time'] = time();
			$insert = Db::table('oyk_school_class')->insert($data);
			if(empty($insert)){
				$this->returnCode(1,'添加失败，请重试');
			}
		}else{
			$data['edit_time'] = time();
			$update = Db::table('oyk_school_class')->where('id',$id)->update($data);
			if(empty($update)){
				$this->returnCode(1,'修改失败，请重试');
			}
		}
		$this->returnCode(0);
	}
	/**
	 * 返回json对象
	 */
	protected function returnCode($code,$data=[],$count=10){
		header('Content-type:application/json');
		if($code == 0){
			$arr = array(
				'code'=>$code,
				'msg'=>'成功',
				'count'=> $count,
				'data' => $data
			);
		}else if($code >= 1 && $code <= 100){
			$arr = array(
				'code'	=>	$code,
				'msg'	=>	$data
			);
		}else{
			$appapi = new AppApi();
			$arr = array(
				'code'=>$code,
				'msg'=>$appapi::errorTip($code)
			);
		}
		echo json_encode($arr);
		if($code != 0){
			exit;
		}
	}
}